package com.ppmall.controller.portal;

import com.ppmall.pojo.Advert;
import com.ppmall.service.IAdvertService;
import com.ppmall.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.util.List;

@RestController
public class AdvertController {

    @Autowired
    IAdvertService iAdvertService;

    @GetMapping("/adverts.do")
    public R getAllAdverts(){
        List<Advert> adverts = iAdvertService.getAllAdverts();
        return R.ok().data("adverts", adverts);
    }

    @DeleteMapping("/adverts.do")
    public R deleteAllAdverts() {
        return iAdvertService.deleteAllAdverts() > 0 ? R.ok() : R.error();
    }

    @GetMapping("/advert.do")
    public R getAdvertById(Integer id){
        Advert advert = iAdvertService.getAdvertById(id);
        return R.ok().data("advert", advert);
    }

    @PutMapping("/advert.do")
    public R updateAdvertById(Advert advert) {
        return iAdvertService.updateAdvertById(advert) > 0 ? R.ok() : R.error();
    }

    @PostMapping("/advert.do")
    public R addAdvert(Advert advert) {
        return iAdvertService.addAdvert(advert) > 0 ? R.ok() : R.error();
    }

    @DeleteMapping("/advert.do")
    public R deleteAdvertById(Integer id){
        return iAdvertService.deleteAdvertById(id) > 0 ? R.ok() : R.error();
    }
}